<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en-US">
<head>
    <meta charset="utf-8">


    <link href="/assets/css/all.css" rel="stylesheet" type="text/css">
    <script async src="/assets/js/all.js"></script>


    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({tex2jax: {inlineMath: [['«', '»']]}});
      MathJax.Hub.config.tex2jax.skipTags = ["script", "noscript", "style", "textarea", "annotation", "annotation-xml"];

    </script>
    <script async src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.2/MathJax.js?config=TeX-AMS_HTML'></script>
    <!-- Google Analytics -->
    <script>
        window.ga = window.ga || function () {
            (ga.q = ga.q || []).push(arguments)
        };
        ga.l = +new Date;
        ga('create', 'UA-58002512-1', 'auto');
        ga('set', 'anonymizeIp', true);
        ga('send', 'pageview');
    </script>
    <script async src='https://www.google-analytics.com/analytics.js'></script>
    <!-- End Google Analytics -->

    <title>Nim by Example - Primitives</title>

    <meta content="nanoc 4.9.9" name="generator">
    <meta content="colorful" http-equiv="Default-Style">
    <meta content="width=device-width, initial-scale=1" name=viewport>
</head>
<body>

<div id="sidebar">
    <div class="abs-hamburger">
        <div class="nav-toggle" onclick="sidebarClick();"><span></span></div>
    </div>
    <nav>
        <ul>
            <li><a href="/getting_started/">Getting Started</a></li>
            <li><a href="/hello_world/">Hello World</a></li>
            <li><a href="/comments/">Comments</a></li>
            <li><a href="/variables/">Variables</a>
                <ul>
                    <li><a href="/variables/result/">Result</a></li>
                    <li><a href="/variables/type_casting_inference/">Type Casting and Inference</a></li>
                </ul>
            </li>
            <li><a href="/if_else_while/">If, Else, While</a></li>
            <li><a href="/case/">Case Statements</a></li>
            <li><a href="/for_iterators/">For Loops &amp; Iterators</a></li>
            <li><a href="/procs/">Procs</a></li>
            <li><a href="/procvars/">First Class Functions</a></li>
            <li><a href="/block/">Blocks</a></li>
            <li><a href="/primitives/">Primitive Types</a></li>
            <li><a href="/types/">Type Aliases</a></li>
            <li><a href="/types/objects/">Object Types</a></li>
            <li><a href="/types/enums/">Enum Types</a></li>
            <li><a href="/types/distinct/">Distinct Types</a></li>
            <li><a href="/strings/">Strings</a></li>
            <li><a href="/arrays/">Arrays</a></li>
            <li><a href="/seqs/">Seqs</a></li>
            <li><a href="/bitsets/">Bitsets</a></li>
            <li><a href="/files/">Files</a></li>
            <li><a href="/json/">JSON</a></li>
            <li><a href="/varargs/">Varargs</a></li>
            <li><a href="/oop/">Object Oriented Programming</a></li>
            <li><a href="/macros/">Macros</a></li>
        </ul>


    </nav>
</div>

<article>
    <h1 id="primitives">Primitives</h1>

    <p>Nim has several primitive types:</p>

    <ul>
        <li>signed integers: <code>int8</code>, <code>int16</code>, <code>int32</code>, <code>int64</code>, and <code>int</code>,
            where <code>int</code> is the same size as a pointer
        </li>
        <li>unsigned integers are similar with <code>u</code> prepended to the type</li>
        <li>floating points numbers: <code>float32</code>, <code>float64</code>, and <code>float</code>, where <code>float</code>
            is the processor’s fastest type
        </li>
        <li>characters: <code>char</code>, which is basically an alias for uint8</li>
    </ul>

    <p>To indicate the size of an integer literal, append <code>u</code> or <code>i</code> and the size you’d like to
        the end. However, usually this is not necessary.</p>

    <p>Integers can also have <code>0[xX]</code>, <code>0o</code>, <code>0[Bb]</code> prepended to indicate a hex,
        octal, or binary literal, respectively. Underscores are also valid in literals, and can help with readability.
    </p>

    <pre><code class="language-nimrod"><span class="k">let</span>
  <span class="n">a</span><span class="p">:</span> <span class="kt">int8</span> <span class="o">=</span> <span
                class="mh">0x7F</span> <span class="c"># Works</span>
  <span class="n">b</span><span class="p">:</span> <span class="n">uint8</span> <span class="o">=</span> <span
                class="mb">0b1111_1111</span> <span class="c"># Works</span>
  <span class="n">d</span> <span class="o">=</span> <span class="mh">0xFF</span> <span class="c"># type is int</span>
  <span class="n">c</span><span class="p">:</span> <span class="n">uint8</span> <span class="o">=</span> <span
                class="mi">256</span> <span class="c"># Compile time error</span></code></pre>

    <p>Precedence rules are the same as in most other languages, but instead of <code>^</code>, <code>&amp;</code>,
        <code>|</code>, <code>&gt;&gt;</code>, <code>&lt;&lt;</code>, the <code>xor</code>, <code>and</code>,
        <code>or</code>, <code>shr</code>, <code>shl</code> operators are used, respectively.</p>

    <pre><code class="language-nimrod"><span class="k">let</span>
  <span class="n">a</span><span class="p">:</span> <span class="kt">int</span> <span class="o">=</span> <span
                class="mi">2</span>
  <span class="n">b</span><span class="p">:</span> <span class="kt">int</span> <span class="o">=</span> <span
                class="mi">4</span>
<span class="n">echo</span> <span class="mi">4</span><span class="o">/</span><span class="mi">2</span></code></pre>
    <pre><code class="language-console"><span class="gp">$</span> nim c -r numbers2.nim
<span class="go">2.0</span></code></pre>
    <p>Another difference that may be surprising is that the <code>/</code> operator returns a floating point result,
        even when the operands are integers. If integer division is needed, the <code>div</code> operator should be
        used.</p>

</article>

<div id=nextprev>
    <a class="text-icon disabled" href="https://please-enable-js/" id=arrow-prev>↽</a>
    <a class="text-icon disabled" href="https://please-enable-js/" id=arrow-next>⇁</a>
</div>

<footer>
    <li><a href="https://github.com/flaviut/nim-by-example">Contribute</a></li>
    <li
    ><a href="#" onclick="toggleDarkMode(); return false;">Toggle dark mode</a></li>

</footer>
</body>
</html>
